package net.caixiaomi.info.Lottery.ui;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;

import net.caixiaomi.info.Lottery.adapter.LotteryPriceTicketAdapter;
import net.caixiaomi.info.Lottery.model.LotteryTicketSchemeEntity;
import net.caixiaomi.info.Lottery.model.LottoTicketSchemeDetail;
import net.caixiaomi.info.R;
import net.caixiaomi.info.adapter.PrintTicketPlanAdapter;
import net.caixiaomi.info.base.BaseActivity;
import net.caixiaomi.info.base.progressbar.CircularProgressBar;
import net.caixiaomi.info.model.BaseCallModel;
import net.caixiaomi.info.model.PrintTicketModel;
import net.caixiaomi.info.net.RequestService;
import net.caixiaomi.info.net.ResponseCallback;
import net.caixiaomi.info.net.ResponseError;
import net.caixiaomi.info.net.RetrofitManage;
import net.caixiaomi.info.util.ToastUtil;
import net.caixiaomi.smartrefresh.layout.SmartRefreshLayout;
import net.caixiaomi.smartrefresh.layout.api.RefreshLayout;
import net.caixiaomi.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;

/**
 * Created by suntian on 2018/8/20.
 */

public class LotteryPriceTicketPlanActivity extends BaseActivity implements OnRefreshListener {

    @BindView(R.id.refresh)
    SmartRefreshLayout mRefresh;
    @BindView(android.R.id.list)
    RecyclerView mListView;
    @BindView(R.id.progress)
    CircularProgressBar mProgress;

    private TextView mName;
    private LotteryPriceTicketAdapter mAdapter;
    private String mSn;
    private String mProgrameSn;

    @Override
    protected int layout() {
        return R.layout.act_default_list;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSn = getIntent().getStringExtra("sn");
        mProgrameSn = getIntent().getStringExtra("psn");
        intView();
        loadData();
    }

    private void intView() {
        try {
            mRefresh.setOnRefreshListener(this);
            View headerView = LayoutInflater.from(this).inflate(R.layout.header_print_ticket_plan, null);
            headerView.findViewById(R.id.title_des).setVisibility(View.GONE);
            headerView.findViewById(R.id.title_des_lottery).setVisibility(View.VISIBLE);
            mName = (TextView) headerView.findViewById(R.id.name);
            mListView.setLayoutManager(new LinearLayoutManager(this));
            mAdapter = new LotteryPriceTicketAdapter(R.layout.lottery_order_item_layout);
            mAdapter.addHeaderView(headerView);
            mAdapter.setEnableLoadMore(false);
            mListView.setAdapter(mAdapter);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void loadData() {
        JSONObject params = new JSONObject();
        params.put("orderSn", mSn);
        params.put("programmeSn", mProgrameSn);
        RequestService requestService = RetrofitManage.getInstance().getRequestService();
        Call<BaseCallModel<LotteryTicketSchemeEntity>> ticketScheme = requestService.getLottoTicketScheme(params);

        ticketScheme.enqueue(new ResponseCallback<BaseCallModel<LotteryTicketSchemeEntity>>() {
            @Override
            public void onSuccess(BaseCallModel<LotteryTicketSchemeEntity> data) {
                mProgress.setVisibility(View.GONE);
                mRefresh.finishRefresh();
                LotteryTicketSchemeEntity model = data.data;
                if (model != null) {
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.append(getString(R.string.C_PLAN_NUM)).append(" :").append(model.getProgrammeSn());
                    mName.setText(stringBuilder);
                    List ll=model.getLottoTicketSchemeDetailDTOs();
                    if(ll==null||ll.size()==0){
                        List<LottoTicketSchemeDetail> lst=new ArrayList();
                        LottoTicketSchemeDetail entity=new LottoTicketSchemeDetail();
                        entity.setStatus(0);
                        lst.add(entity);
                        mAdapter.setNewData(lst);
                    }else{
                        mAdapter.setNewData(ll);
                    }
                }
            }

            @Override
            public void onFail(ResponseError error) {
                mProgress.setVisibility(View.GONE);
                mRefresh.finishRefresh();
                if (error.getCode() >= 300000 && error.getCode() <= 310000) {
                    ToastUtil.s(error.getMessage());
                }
            }

            @Override
            public void onError() {
                mProgress.setVisibility(View.GONE);
                mRefresh.finishRefresh();
            }
        });
    }

    public void onRefresh() {
        loadData();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        onRefresh();
    }
}
